package vis;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

public class TextLabel implements Drawable {
	private Color color = new Color(0x003000);
	private Color hlColor = Color.ORANGE;
	private boolean hl = false;
	private boolean visible = true;
	private double x = 0.0, y = 0.0;
	private String text = "";
	private static final Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
	
	public TextLabel() {}
	
	public TextLabel(String text)
	{
		this.text = text;
	}
	
	private static final Color toolTipC = new Color(0xFFFFA0);
	
	@Override
	public void draw(Graphics2D g) {
		if(text.length() == 0) return;
		
		g.scale(1.0, -1.0);
		//g.setColor(Color.black);
		//g.drawString(text, (float)x + 0.5f, (float)y + 0.5f);
		FontMetrics fm = g.getFontMetrics();
		Rectangle2D bounds = fm.getStringBounds(text, g);
		bounds.setRect(bounds.getX(), bounds.getY(), bounds.getWidth() * 1.6, bounds.getHeight() + 6.0);
		
		g.setColor(Color.gray);
		g.drawLine((int)x, (int)-y, (int)x + 30, (int)-y - 30);
		
		g.setColor(toolTipC);
		g.fillRect((int)x - 3 + 30, -30 + (int)-y - (int)bounds.getHeight() + 3, (int)bounds.getWidth(), (int)bounds.getHeight());
		g.setColor(Color.black);
		g.drawRect((int)x - 3 + 30, -30 + (int)-y - (int)bounds.getHeight() + 3, (int)bounds.getWidth(), (int)bounds.getHeight());
		
		g.setColor(hl ? hlColor : color);
		g.setFont(font);
		g.drawString(text, (float)x + 30, -(float)y - 30);
		g.scale(1.0, -1.0);
	}
	
	public void setText(String text)
	{
		this.text = text;
	}
	
	public String getText()
	{
		return text;
	}

	@Override
	public Color getColor() {
		return color;
	}

	@Override
	public boolean isVisible() {
		return visible;
	}

	@Override
	public void resetHighlight() {
		hl = false;
	}

	@Override
	public void setColor(Color c) {
		color = c;
	}

	@Override
	public void setHighlight(Color c) {
		hlColor = c;
		hl = true;
	}

	@Override
	public void setPosition(double x, double y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public void setVisible(boolean vis) {
		visible = vis;
	}

	@Override
	public double getPosX() {
		return x;
	}

	@Override
	public double getPosY() {
		return y;
	}
	
	@Override
	public Color getHighlightColor() {
		return hlColor;
	}

	@Override
	public boolean isHighlighted() {
		return hl;
	}


}
